<template>
  <div class="layout">
    <el-container v-if="showMenu" class="container">
      <el-aside class="aside">
        <div class="head">
          <div>
            <img
                src="https://tse1-mm.cn.bing.net/th/id/R-C.589f7c73aa489b9b07843ac2070e1e21?rik=uIGzCk8C2ee9Ag&riu=http%3a%2f%2fwww.kuaipng.com%2fUploads%2fpic%2fw%2f2021%2f03-31%2f98709%2fwater_98709_698_698_.png&ehk=E8bzS7h8HKPMOocjtzXljBWDOZPsT9AS06PlQvLk%2bPs%3d&risl=&pid=ImgRaw&r=0"
                alt="logo">
            <span>教学评价系统</span>
          </div>
        </div>
        <div class="line"/>
        <el-menu
            :default-openeds="defaultOpen"
            background-color="#222832"
            text-color="#fff"
            :router="true"
            :default-active='currentPath'
        >
          <el-submenu index="2" v-if="userInfo.roleIds.includes(2)">
            <template #title>
              <span>教师中心</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/evaluationResults"><i class="el-icon-picture"/>评价结果</el-menu-item>
              <el-menu-item index="/startPlan?type=teacher"><i class="el-icon-picture"/>开始评价</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="2" v-if="userInfo.roleIds.includes(1)">
            <template #title>
              <span>评价中心</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/courseOrder"><i class="el-icon-picture"/>课程订阅</el-menu-item>
              <el-menu-item index="/startPlan"><i class="el-icon-picture"/>开始评价</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="3" v-if="userInfo.roleIds.includes(3)">
            <template #title>
              <span>数据管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/menu"><i class="el-icon-menu"/>菜单管理</el-menu-item>
              <el-menu-item index="/role"><i class="el-icon-s-goods"/>角色管理</el-menu-item>
              <el-menu-item index="/user"><i class="el-icon-user-solid"/>用户管理</el-menu-item>
              <el-menu-item index="/topic"><i class="el-icon-user-solid"/>问题管理</el-menu-item>
              <el-menu-item index="/plan"><i class="el-icon-s-order"/>评价计划管理</el-menu-item>
              <el-menu-item index="/course"><i class="el-icon-s-order"/>课程管理</el-menu-item>
              <el-menu-item index="/evaluationStatus"><i class="el-icon-star-on"/>评价回退</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
          <el-submenu index="4">
            <template #title>
              <span>系统管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/account"><i class="el-icon-lock"/>修改密码</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container class="content">
        <Header/>
        <div class="main">
          <router-view/>
        </div>
        <Footer/>
      </el-container>
    </el-container>
    <el-container v-else class="container">
      <router-view/>
    </el-container>
  </div>
</template>

<script>
import {onMounted, onUnmounted, reactive, toRefs} from 'vue'
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import {useRouter} from 'vue-router'
import {localGet, pathMap} from '@/utils'
import axios from "@/utils/axios";

export default {
  name: 'App',
  components: {
    Header,
    Footer
  },
  setup() {
    const noMenu = ['/login',"/registered"]
    const router = useRouter()
    const state = reactive({
      defaultOpen: ['1', '2', '3', '4'],
      showMenu: true,
      currentPath: '/user',
      count: {
        number: 1
      },
      userInfo: {
        roleIds: []
      },
    })
    // 监听浏览器原生回退事件
    if (window.history && window.history.pushState) {
      history.pushState(null, null, document.URL);
      window.addEventListener('popstate', () => {
        if (!localGet('token')) {
          state.showMenu = false
        }
      }, false);
    }
    const unwatch = router.beforeEach((to, from, next) => {
      if (to.path === '/login' || to.path ==='/registered') {
        // 如果路径是 /login 则正常执行
        next()
      } else {
        // 如果不是 /login，判断是否有 token
        if (localGet('token')==null) {
          // 如果没有，则跳至登录页面
          next({path: '/login'})
        } else {
          // 否则继续执行
          next()
        }
      }
      state.showMenu = !noMenu.includes(to.path)
      state.currentPath = to.path
      document.title = pathMap[to.name]
    })

    onUnmounted(() => {
      unwatch()

    })
    onMounted(()=>{
       getUserInfo()
    })

    const getUserInfo = async () => {
      state.userInfo = await axios.get('/evaluate/user/info')
      console.log(state.userInfo.roleIds.includes(1))
    }

    return {
      ...toRefs(state)
    }
  }
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
  background-color: #ffffff;
}

.container {
  height: 100vh;
}

.aside {
  width: 200px !important;
  background-color: #222832;
  overflow: hidden;
  overflow-y: auto;
  -ms-overflow-style: none;
}

.aside::-webkit-scrollbar {
  display: none;
}

.head {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
}

.head > div {
  display: flex;
  align-items: center;
}

.head img {
  width: 50px;
  height: 50px;
  margin-right: 10px;
}

.head span {
  font-size: 20px;
  color: #ffffff;
}

.line {
  border-top: 1px solid hsla(0, 0%, 100%, .05);
  border-bottom: 1px solid rgba(0, 0, 0, .2);
}

.content {
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  overflow: hidden;
}

.main {
  height: calc(100vh - 100px);
  overflow: auto;
  padding: 10px;
}
</style>
<style>
body {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.el-menu {
  border-right: none !important;
}

.el-submenu {
  border-top: 1px solid hsla(0, 0%, 100%, .05);
  border-bottom: 1px solid rgba(0, 0, 0, .2);
}

.el-submenu:first-child {
  border-top: none;
}

.el-submenu [class^="el-icon-"] {
  vertical-align: -1px !important;
}

a {
  color: #409eff;
  text-decoration: none;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}

.el-popper__arrow {
  display: none;
}
</style>
